package com.tvserials.notification.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.Filter;
import android.widget.ImageView;
import com.tvserials.notification.R;
import com.tvserials.notification.utils.ResourceUtils;
import com.tvserials.notification.dto.Serial;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * Created by oleksandr.dokienko on 3/27/2014.
 */
public class TvSerialsArrayAdapter extends ArrayAdapter<Serial> {

    private List<Serial> data;
    private List<Serial> origData;

    public TvSerialsArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public TvSerialsArrayAdapter(Context context, int textViewResourceId, List<Serial> list) {
        super(context, textViewResourceId, list);
        this.data = list;
        this.origData = new ArrayList<Serial>(this.data);
    }

    public List<Serial> getData() {
        return data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflator.inflate(R.layout.serials_list_item, null);

            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) view.findViewById(R.id.serialsIcon);
            viewHolder.checkedTextView = (CheckedTextView) view.findViewById(R.id.checkText);
            viewHolder.checkedTextView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    CheckedTextView checkedTextView = (CheckedTextView) v;
                    checkedTextView.setChecked(!checkedTextView.isChecked());
                    Serial element = (Serial) v.getTag();
                    element.setSelected(checkedTextView.isChecked());
                }
            });
            viewHolder.checkedTextView.setTag(data.get(position));
            view.setTag(viewHolder);
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkedTextView.setTag(data.get(position));
        }
        Serial serialVO = data.get(position);
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.checkedTextView.setText(serialVO.getShowName());
        holder.checkedTextView.setChecked(serialVO.isSelected());
        holder.imageView.setImageResource(ResourceUtils.getIconResourceId(serialVO.getSourceType()));
        showAnimation(view);
        return view;
    }

    private void showAnimation(View rowView) {
        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.fadein);
        if (animation != null) {
            animation.setDuration(400);
            rowView.setAnimation(animation);
        }
    }

    @Override
    public Filter getFilter() {
        return new Filter() {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                constraint = constraint.toString().toLowerCase();
                FilterResults result = new FilterResults();

                if (constraint != null && constraint.toString().length() > 0) {
                    List<Serial> founded = new ArrayList<Serial>();
                    for (Serial item : origData) {
                        if (item.getShowName().toLowerCase().contains(constraint)) {
                            founded.add(item);
                        }
                    }

                    result.values = founded;
                    result.count = founded.size();
                } else {
                    result.values = origData;
                    result.count = origData.size();
                }
                return result;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                clear();
                for (Serial item : (List<Serial>) results.values) {
                    add(item);
                }
                notifyDataSetChanged();
            }
        };
    }

    @Override
    public void sort(Comparator<? super Serial> comparator) {
        super.sort(comparator);
        notifyDataSetChanged();
    }

    private static class ViewHolder {
        public CheckedTextView checkedTextView;
        public ImageView imageView;
    }
}
